package Table;

import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Vector;

import javax.swing.JOptionPane;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.JTableHeader;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableColumn;
import javax.swing.table.TableColumnModel;
import javax.swing.table.TableRowSorter;

import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;

import org.w3c.dom.events.EventTarget;
import org.w3c.dom.views.AbstractView;

import Frame.AlterInfoDialog;
import Rule.NameTag;

public class MyTable extends JTable{

	private static MyTable table = null;
	private static JTable table_2 =null;

	/**
	 * 返回指定列名的表格
	 * @param tavleValues 表格内容向量
	 * @param isSetRowSorter 是否排序
	 * @param isUpdateTabPanel 是否是更面tab，如是，测隐藏第三列并添加鼠标事件监听器
	 * @return MyTable table
	 */
	public  static MyTable getTable(Vector tavleValues, boolean isSetRowSorter ,
			boolean isUpdateTabPanel){
		
		return extracted_1(tavleValues, isSetRowSorter, isUpdateTabPanel);	
	}

	
	/**
	 * 返回指定路径的表格
	 * @param filePath 文件路径
	 * @return Jtable table
	 */
	public static JTable getTable(String filePath){

		return extracted_2(filePath);	
		
	}
	
	/**
	 * 返回指定列名的表格的方法
	 * @param tavleValues
	 * @param isSetRowSorter
	 * @param isUpdateTabPanel
	 * @return
	 */
	private static MyTable extracted_1(Vector tavleValues,
			boolean isSetRowSorter, boolean isUpdateTabPanel) {
		
		Vector columName = null;		
		columName = new Vector();
		columName.add(NameTag.common_Name);
		columName.add(NameTag.common_No);
		columName.add(NameTag.common_Pri);
		if(isUpdateTabPanel){ //更新界面第四序号
			columName.add(NameTag.common_Num);
		}
		MyTableModel myTableModel = new MyTableModel(columName,tavleValues);	
		table = new MyTable();
		table.setModel(myTableModel);
		table.getTableHeader().setReorderingAllowed(false); //不可重排
		table.getTableHeader().setResizingAllowed(false);//不可调整列宽
		if(isSetRowSorter){ 
			//是否需要排序
			table.setRowSorter(new TableRowSorter(myTableModel));
		}	
		if(isUpdateTabPanel){ 
			//更新界面第五列
			table.hideTableColumn(3);
			table.addMouseListener(new MouseAdapter() { //鼠标事件监听器
				 public void mouseClicked(MouseEvent e) {
					 if(e.getClickCount() == 2){
						 sendToAlterDialog(table.getSelectedRow());
					 }				 
				 }
			});
		}
		return table;
	}		
	
	/**
	 * 返回指定路径的表格的方法
	 * @param filePath
	 * @return
	 */
	private static JTable extracted_2(String filePath) {

		File file=new File(filePath);
		Workbook book = null;
		try {		
			book = Workbook.getWorkbook(file);
		} catch (BiffException e) {		
			e.printStackTrace();
			JOptionPane.showMessageDialog(null, "选择的xls文件错误","错误",JOptionPane.ERROR_MESSAGE);
		} catch (IOException e) {			
			e.printStackTrace();		
		}   

		Sheet sheet = book.getSheet(0); 
		int rows = sheet.getRows();//获取总的行数  
		int cols = sheet.getColumns();//获取总的列数  
		if (rows == 0 && cols == 0){ //如工作薄无任何内容则返回 table=null
			
			return table_2 = null; 		
		}else{		
			
			String[][] str = new String[rows][cols];
			//读取单元格内容并存放到二维数组中 默认从第一行第一列读取   
			for(int i=0;i<str.length;i++){			
			         for(int j=0;j<str[i].length;j++){  	        	 
			          Cell cell = sheet.getCell(j,i);  
			          str[i][j] = cell.getContents().replace(",", "").trim();            
			         }  		         
			}
			Vector columnNameV=new Vector<>();
			for(int i=0;i<cols;i++){
				
				columnNameV.add(String.valueOf(str[0][i]));			
			}
			Vector<Vector> tableValueV=new Vector<>();
			for(int j=1;j<rows;j++){
				
				Vector rowV=new Vector<>();
				for(int x=0;x<cols;x++){
					
					rowV.add(str[j][x]);			
				}
				tableValueV.add(rowV);
			}
			table_2 = new JTable(tableValueV,columnNameV);
			table_2.getTableHeader().setReorderingAllowed(false);//不可移动重排    
			return table_2;
		}
	}	
	
	/**
	 * 表头不可重排
	 */
	public JTableHeader getTableHeader(){
		
		JTableHeader tableHeader = super.getTableHeader();
		tableHeader.setReorderingAllowed(false);		
		return tableHeader;	
	}	
	
	/**
	 * 单元格对齐方法
	 */
	public TableCellRenderer getDefaultRenderer(Class<?>columnClass)
    {
        DefaultTableCellRenderer cr =(DefaultTableCellRenderer)super.getDefaultRenderer(columnClass);
        cr.setHorizontalAlignment(DefaultTableCellRenderer.LEFT);  //单元格内容左对齐
        return cr;
    }
	
	/**
	 * 表格隐藏某列
	 * @param columnIndex 第几列
	 */
	protected void hideTableColumn(int columnIndex){  
		TableColumn tc= (TableColumn)super.getColumnModel().getColumn(columnIndex); 
	    tc.setMaxWidth(0); 
	    tc.setPreferredWidth(0); 
	    tc.setMinWidth(0); 
	    tc.setWidth(0); 
	    super.getTableHeader().getColumnModel().getColumn(columnIndex).setMaxWidth(0); 
	    super.getTableHeader().getColumnModel().getColumn(columnIndex).setMinWidth(0);       
	}  

	
	/**
	 * 发送数据到修改对话框
	 * @param iRowIndex 行号
	 */
	protected static void sendToAlterDialog(int iRowIndex){
		String sName = table.getValueAt(iRowIndex, 0).toString();
		String sNo = table.getValueAt(iRowIndex, 1).toString();
		String sPri = table.getValueAt(iRowIndex, 2).toString();
		String sNum = table.getValueAt(iRowIndex, 3).toString();

		new AlterInfoDialog(sName, sNo, sPri, sNum);
					
	}
	
}
